package com.gx.smart.building.ui.maintain.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.building.PlanTaskApiService
import com.gx.smart.building.admin.SysRegionApiService
import com.gx.smart.building.ui.common.viewmodel.CommonViewModel
import com.gx.smart.building.ui.maintain.event.FinishedQuantityEvent
import com.gx.smart.building.ui.maintain.event.MaintainListEvent
import com.gx.smart.building.ui.maintain.event.TaskInCompleteListEvent
import com.gx.smart.building.ui.maintain.event.UnfinishedQuantityEvent
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.AddPlanTaskEventResp
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.HandleTaskResp
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskAssetDetailResp
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskAssetListResp
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.QueryPlanTaskListResp
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetDto
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetFileDto
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskAssetItemResultDto
import com.huishi.gx.ib.grpc.lib.ib.app.plantask.v1.dto.PlanTaskDto
import com.huishi.gx.ib.grpc.lib.sysuser.sysregionapi.SysRegionDto
import com.huishi.gx.ib.grpc.lib.sysuser.sysregionapi.SysRegionQueryResponse
import org.greenrobot.eventbus.EventBus

/**
 *@author: xiaosy
 *时间: 2022/3/22
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class MaintainViewModel(application: Application) : CommonViewModel(application) {
    private val quantity: MutableLiveData<List<Long>> = MutableLiveData()
    private val taskQuantity: MutableLiveData<List<Long>> = MutableLiveData()

    val detail: MutableLiveData<PlanTaskAssetDto> = MutableLiveData()
    val detailFileList: MutableLiveData<List<PlanTaskAssetFileDto>> = MutableLiveData()
    val detailResultList: MutableLiveData<List<PlanTaskAssetItemResultDto>> =
        MutableLiveData()

    private val handleSuccess: MutableLiveData<Boolean> = MutableLiveData()

    private val regionList: MutableLiveData<List<SysRegionDto>> = MutableLiveData()
    private val forwardSuccess: MutableLiveData<Boolean> = MutableLiveData()
    /**
     * 移动端计划列表
     */
    fun queryPlanTaskListOnFirst(currentPage: Int): LiveData<List<Long>> {
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        PlanTaskApiService.getInstance()
            .queryPlanTaskList(
                comQuery,
                2,
                2,
                "",
                0,
                0,
                object : CallBack<QueryPlanTaskListResp?>() {
                    override fun callBack(result: QueryPlanTaskListResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            val list: List<Long> =
                                listOf(result.unfinishedQuantity, result.finishedQuantity)
                            quantity.value = list
                            EventBus.getDefault()
                                .postSticky(MaintainListEvent(result.planTaskDtoList))
                        } else {
                            handleServiceException(comResp)
                        }
                    }

                })
        return quantity
    }

    /**
     * 移动端计划列表
     */
    fun queryPlanTaskList(
        currentPage: Int,
        status: Int,
        taskCode: String,
        handleTimeStart: Long,
        handleTimeEnd: Long
    ): LiveData<List<PlanTaskDto>> {
        val list: MutableLiveData<List<PlanTaskDto>> = MutableLiveData()
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        PlanTaskApiService.getInstance()
            .queryPlanTaskList(
                comQuery,
                2,
                status,
                taskCode,
                handleTimeStart,
                handleTimeEnd,
                object : CallBack<QueryPlanTaskListResp?>() {
                    override fun callBack(result: QueryPlanTaskListResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            list.value = result.planTaskDtoList
                            //状态：1-已完成，2-未完成
                            when (status) {
                                2 -> EventBus.getDefault()
                                    .post(UnfinishedQuantityEvent(result.unfinishedQuantity))

                                1 -> EventBus.getDefault()
                                    .post(FinishedQuantityEvent(result.finishedQuantity))
                            }
                        } else {
                            handleServiceException(comResp)
                        }
                    }

                })
        return list
    }

    /**
     * 移动端任务列表
     */
    fun queryPlanTaskAssetListOnFirst(plantTaskId: String): LiveData<List<Long>> {
        val comQuery = ComQuery.newBuilder()
            .setPage(1)
            .setPageSize(10)
            .build()
        PlanTaskApiService.getInstance().queryPlanTaskAssetList(
            comQuery, 2, 2, plantTaskId, "", "",
            object : CallBack<QueryPlanTaskAssetListResp>() {
                override fun callBack(result: QueryPlanTaskAssetListResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        val quantityList: List<Long> =
                            listOf(result.unfinishedQuantity, result.finishedQuantity)
                        taskQuantity.value = quantityList
                        EventBus.getDefault()
                            .postSticky(TaskInCompleteListEvent(result.planTaskAssetDtoList))
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return taskQuantity
    }

    /**
     * 移动端任务列表
     *   plan_type 计划类型：1-巡检计划，2-保养计划
     */
    fun queryPlanTaskAssetList(
        currentPage: Int,
        status: Int,
        plantTaskId: String,
        assetName: String,
        areaId: String
    ): LiveData<List<PlanTaskAssetDto>> {
        val taskList: MutableLiveData<List<PlanTaskAssetDto>> = MutableLiveData()
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()
        PlanTaskApiService.getInstance().queryPlanTaskAssetList(
            comQuery, 2, status, plantTaskId, assetName, areaId,
            object : CallBack<QueryPlanTaskAssetListResp>() {
                override fun callBack(result: QueryPlanTaskAssetListResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        taskList.value = result.planTaskAssetDtoList
                        //状态：1-已完成，2-未完成
                        when (status) {
                            2 -> EventBus.getDefault()
                                .post(UnfinishedQuantityEvent(result.unfinishedQuantity))

                            1 -> EventBus.getDefault()
                                .post(FinishedQuantityEvent(result.finishedQuantity))
                        }
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return taskList
    }


    /**
     * 移动端任务详情
     */
    fun queryPlanTaskAssetDetail(planTaskId: String, assetId: String) {
        PlanTaskApiService.getInstance()
            .queryPlanTaskAssetDetail(
                planTaskId,
                assetId,
                object : CallBack<QueryPlanTaskAssetDetailResp?>() {
                    override fun callBack(result: QueryPlanTaskAssetDetailResp?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            //计划任务列表
                            detail.value = result.planTaskAssetDto
                            //处理结果文件
                            detailFileList.value = result.taskAssetFileDtoList
                            detailResultList.value = result.taskAssetItemResultDtoList
                        } else {
                            handleServiceException(comResp)
                        }
                    }
                })
    }

    /**
     * 移动端处理任务
     */
    fun handleTask(
        planTaskAssetDto: PlanTaskAssetDto,
        planTaskAssetFileDto: List<PlanTaskAssetFileDto>,
        planTaskAssetItemResultDto: List<PlanTaskAssetItemResultDto>
    ): LiveData<Boolean> {
        PlanTaskApiService.getInstance().handleTask(planTaskAssetDto,
            planTaskAssetFileDto,
            planTaskAssetItemResultDto,
            object : CallBack<HandleTaskResp>() {
                override fun callBack(result: HandleTaskResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        handleSuccess.value = false
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        handleSuccess.value = true
                    } else {
                        handleServiceException(comResp)
                        handleSuccess.value = false
                    }
                }
            })
        return handleSuccess
    }

    //查询
    fun sysRegionQuery(): LiveData<List<SysRegionDto>> {
        SysRegionApiService.getInstance()
            .sysRegionQuery(object : CallBack<SysRegionQueryResponse>() {
                override fun callBack(result: SysRegionQueryResponse?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        regionList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return regionList
    }

    //转派
    fun forwardTask(
        planTaskId: String,
        opinion: String,
        assignUserName: String,
        userId: String
    ): LiveData<Boolean> {
        PlanTaskApiService.getInstance().forwardTask(
            planTaskId,
            opinion,
            assignUserName,
            userId,
            object : CallBack<AddPlanTaskEventResp>() {
                override fun callBack(result: AddPlanTaskEventResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        forwardSuccess.value = true
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return forwardSuccess
    }
}